package xyj.sparrow.discovery.single.bean;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;
import xyj.sparrow.common.discovery.SparrowServiceInstance;
import xyj.sparrow.discovery.api.SparrowDiscovery;
import xyj.sparrow.discovery.single.config.SingleConf;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;


/**
 * @author 江南小俊
 * @since 2021/6/19 上午9:56
 **/
@Component
@Slf4j
public class LocalSparrowDiscovery implements SparrowDiscovery {
    @Resource
    private SingleConf singleConf;
    private SparrowServiceInstance sparrowServiceInstance;

    @Override
    public Object register(String serviceName, String address, int port, Object payload) throws Exception {
        return null;
    }

    @Override
    public void unregister(List<String> serviceIds) {

    }

    @Override
    public SparrowServiceInstance getInstance(String serviceName) throws Exception {
        if (sparrowServiceInstance == null) {
            sparrowServiceInstance = new SparrowServiceInstance();
            sparrowServiceInstance.setName(serviceName);
            sparrowServiceInstance.setAddress(singleConf.getAddress());
            sparrowServiceInstance.setPort(singleConf.getPort());
        } else {
            return sparrowServiceInstance;
        }

        return null;
    }

    @Override
    public void addListener(String serviceName, String providerStrategy, Consumer consumer) throws Exception {

    }

    @Override
    public void addListener(String serviceName, String providerStrategy) throws Exception {

    }

    @Override
    public void addListener(String serviceName) throws Exception {

    }

    @Override
    public List<SparrowServiceInstance> listInstance(String serviceName) throws Exception {
        return null;
    }
}
